/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author marwa
 */
public class NewSequencer extends javax.swing.JInternalFrame {

    /**
     * Creates new form NewSequencer
     */
    
    String ImageTitle;
    Boolean flag;
    Boolean valid;
    String sequence;
    WorkArea obj;
    Pattern pattern;
    Matcher matcher;
        
    private static final String SEQUENCE_PATTERN ="(A*|C*|G*|T*|a*|c*|g*|t*)*";
    
    
    public NewSequencer() {
        initComponents();
        pattern = Pattern.compile(SEQUENCE_PATTERN);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sequencerTitle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        sequenceArea = new javax.swing.JTextArea();
        generateButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setClosable(true);
        setTitle("New Sequencer");

        jLabel1.setText("Title:");

        sequencerTitle.setText("Untitled");

        jLabel2.setText("Sequence:");

        sequenceArea.setColumns(20);
        sequenceArea.setRows(5);
        jScrollPane1.setViewportView(sequenceArea);

        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(generateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addGap(91, 91, 91))
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(48, 48, 48)
                        .addComponent(sequencerTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(sequencerTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(generateButton))
                .addGap(36, 36, 36))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed

        ImageTitle = CheckTitle(sequencerTitle.getText());
        sequence = sequenceArea.getText();
        flag = ValidateSequenceArea(sequence);
        if(flag== true)
        {
            valid = validateSequence(sequence);
            if(valid == true)
            {
                obj = new WorkArea(ImageTitle);
                obj.setLayout(null); ///////////////////////// to create it 3ala 7asab 8 wala 16
                obj.setVisible(true);
                obj.setSize(1000, 590);
                obj.setLocation(20,60);
                obj.toFront();
                
                sequencerToolBox stb = new sequencerToolBox();
                stb.setVisible(true);
                stb.setSize(500, 55);
                stb.setLocation(20, 0);
                stb.toFront();       
                
                super.getDesktopPane().add(obj);       
                super.getDesktopPane().add(stb);       
                
                this.generateSequence(sequence);
                this.setVisible(false);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid sequence of characters!", "Alert", JOptionPane.ERROR_MESSAGE);
            }
                
        }
        else
        {
            JOptionPane.showMessageDialog(null, "No Sequence to be generated !", "Alert", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_generateButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton generateButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea sequenceArea;
    private javax.swing.JTextField sequencerTitle;
    // End of variables declaration//GEN-END:variables

private String CheckTitle(String text) {
        if (text.equals("Untitled") || text.equals("")) {
            return "Untitled";
        } else {
            return text;
        }

    }

    private Boolean ValidateSequenceArea(String text) {
        if(text.equals("") || text.equals(null))
        {
            return false;
        }
        else
            return true;
    }

    private Boolean validateSequence(String text) {
        matcher = pattern.matcher(text);
        return matcher.matches();
    }

    private void generateSequence(String sequence) {
        // here is the processing hna harsem 3ala el obj frame 
    }
}
